package com.lipaluma.examples.java.custom;

import java.lang.reflect.Field;

import com.lipaluma.context.Context;
import com.lipaluma.field.mapper.FieldMapper;
import com.lipaluma.field.mapper.FieldMergeable;
import com.lipaluma.field.mapper.initializer.FieldMapperInitializer;
import com.lipaluma.field.mapper.mapping.Column;
import com.lipaluma.field.mapper.mapping.MappingInfo;
import com.lipaluma.field.merge.FieldMergedFunction;
import com.lipaluma.io.Row;
import com.lipaluma.parser.marshalling.ObjectMarshalled;

public class ColumnAmountTTC implements FieldMapper, FieldMergeable, FieldMapperInitializer {

	private Field field;
	private Column column;
	private double tva = 19.60;
	private FieldMergedFunction fieldMergedFunction;

	public ColumnAmountTTC(Column column) {
		this.column = column;
	}
	@Override
	public void init(Context context, Field field) {
		this.field = field;
	}

	@Override
	public Object unmarshal(Row row) {
		String value = row.extractValue(column);
		
		double amountNet = Double.parseDouble(value);
		return amountNet+(amountNet*tva/100);
	}
	
	public String getDescription() {
		return "colonne montant TTC";
	}

	public Field getField() {
		return field;
	}

	@Override
	public MappingInfo[] getMappings() {
		return new MappingInfo[]{column};
	}
	@Override
	public void marshalValue(Object value, ObjectMarshalled marshal) {}
	
	@Override
	public boolean hasToBeMerged() {
		return this.fieldMergedFunction != null;
	}

	@Override
	public FieldMergedFunction mergedBy() {
		return fieldMergedFunction;
	}
	
	@Override
	public void mergeWith(FieldMergedFunction infos) {
		this.fieldMergedFunction = infos;
	}
}
